//
//  BIImageBrowserViewController.swift
//  TicketSystem
//
//  Created by ET|冰琳 on 2017/2/24.
//  Copyright © 2017年 UK. All rights reserved.
//

import Foundation
import SDWebImage

extension BIImageBrowserViewController {
    
    static func maker() -> BIImageBrowserViewController {
        
        let browser = BIImageBrowserViewController()
        
        
        browser.browserView.cellConfiguration = {[weak browser] (cell, index) in
            
            if let b_browser = browser, let i = index {
                
                if let cell = cell as? BIImageBrowserCollectionViewCell {
                    
                    if let url = b_browser.browserView.dataArr[i.row] as? String{
                        
                        var placeholderImage: UIImage? = nil
                        
                        if let smallImages = b_browser.browserView.smallImages {
                            
                            if smallImages.count >= i.row + 1,
                                let small = b_browser.browserView.smallImages[i.row] as? String,
                                let key = SDWebImageManager.shared().cacheKey(for: URL(string: small)) {
                                    placeholderImage = SDImageCache.shared().imageFromCache(forKey: key)
                            }
                        }
                        cell.indicator.startAnimating()
                        cell.imageView.sd_setImageWithPreviousCachedImage(with: URL(string: url), placeholderImage: placeholderImage, options: SDWebImageOptions(rawValue: 0), progress: { (receive, expect, url) in
                        }, completed: { (img, error, type, url) in
                            cell.indicator.stopAnimating()
                        })
                    }
                }
            }
        }
        browser.browserView.pageStyle = iCarrouselPageStyleLabel
        browser.browserView.iPageLabel.textAlignment = .center
        browser.browserView.iPageLabel.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        browser.browserView.iPageLabel.textColor = UIColor.white
        
        return browser
    }
}
